package net.bingosoft.common.config;

import java.util.Properties;

import net.bingosoft.common.BeanHelper;
import net.bingosoft.common.EncodedProperties;


/**
 * 2007-8-23 下午12:15:32
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:系统配置文件,可以将系统中易变的东西,写入config/config.properties中,然后通过该类读取内容
 *        该配置文件支持动态修改及中文字符,目前已支持目录级别的配置
 * ChangeLog:
 */
public class Config {
	
	private static Properties config = EncodedProperties.getInstance("classpath*:/config/**/*.properties",true);

	
    public static Properties getProperties() {
    	Properties p = new Properties();
    	EncodedProperties cfg = (EncodedProperties)config;
    	for(Properties sub : cfg.getSubProperties()){
    		p.putAll(sub);
    	}
		return p;
	}

	/**
     * @return the string value bound with the key.
     */
    public static String getString(String key) {
        return config.getProperty(key);
    }
    
    /**
     * @param key 
     * @param defaultValue 
     * @return the string value bound with the key.
     */
    public static String getString(String key,String defaultValue) {
        String value = getString(key);
        if(null == value || "".equals(value.trim())){
            return defaultValue;
        }else{
            return value;
        }
    }    
    
    /**
     * 
     * @param property
     * @return
     */
    public static boolean getBoolean(String property) {
        return Boolean.valueOf(getString(property)).booleanValue();
    }

    /**
     * 
     * @param property
     * @param defaultValue
     * @return
     */
    public static boolean getBoolean(String property, boolean defaultValue) {
        String setting = getString(property);
        return (setting == null) ? defaultValue : Boolean.valueOf(setting).booleanValue();
    }    
    
    /**
     * 
     * @param property
     * @param defaultValue
     * @return
     */
    public static Integer getInt(String property, int defaultValue) {
        String propValue = getString(property);
        return BeanHelper.getInt(propValue, defaultValue);
    }

    /**
     * 
     * @param property
     * @param defaultValue
     * @return
     */
    public static Long getLong(String property, long defaultValue) {
        String propValue = getString(property);
        return BeanHelper.getLong(propValue, defaultValue);
    }

    /**
     * 
     * @param property
     * @param defaultValue
     * @return
     */
    public static byte getByte(String property, byte defaultValue) {
        String propValue = getString(property);
        return (propValue == null) ? defaultValue : Byte.parseByte(propValue);
    }
}
